package cn.edu.zime.bigdata.controller;

import cn.edu.zime.bigdata.anotations.AnatationTest;
import cn.edu.zime.bigdata.dao.mapper.TeacherMapper;
import cn.edu.zime.bigdata.dao.model.CountValue;
import cn.edu.zime.bigdata.dao.model.Student;
import cn.edu.zime.bigdata.dao.model.Teacher;
import cn.edu.zime.bigdata.dao.model.Teachers;
import org.apache.commons.io.FileUtils;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.Calendar;
import java.util.List;

@RestController
public class TeacherController {
    static int yf[]={0,31,29,31,30,31,30,31,31,30,31,30,31};
    @Autowired
    TeacherMapper teacherService;
    @GetMapping("/addteacher")
    @AnatationTest
    public int insertTeacher(@RequestParam(value = "key1") String id,
                             @RequestParam(value = "key2") String name,
                             @RequestParam(value = "key3") String genda,
                             @RequestParam(value = "key4") String position,
                             @RequestParam(value = "key5") String subject,
                             @RequestParam(value = "key6") String birthdate) {
        int nian=Integer.parseInt(birthdate.substring(0,4));
        int y=Integer.parseInt(birthdate.substring(5,7));
        int r=Integer.parseInt(birthdate.substring(8,10));
        if(nian%400==0||(nian%4==0&&nian%100!=0))yf[2]=29;
        else yf[2]=28;
        r++;
        if(r>yf[y]){
            y++;
            if(y==13){
                nian++;
                y=1;
                r=1;
            }else {
                r=1;
            }
        }
        System.out.printf("%s\n%s\n%s\n%s\n%s\n%s\n", id, name, genda, position, subject, nian+"-"+y+"-"+r);
        System.out.println(birthdate);
//        System.out.println(birthdate.getClass());
        Calendar cal=Calendar.getInstance();
        return teacherService.insertTeacher(new Teacher(id, name, genda, position, subject, nian+"-"+y+"-"+r,(cal.get(Calendar.YEAR)-nian)+""));
    }
    @AnatationTest
    @GetMapping("/getAllTeachers")
    public List<Teacher> selectAllTeachers() {
        System.out.println("-----select--------");
        return teacherService.selectAllTeachers();
    }

    @GetMapping("/getTeachers")
    public List<Teacher> selectTeachers(@RequestParam(value = "key1") String input) {
        System.out.println("input:" + input);
        return teacherService.selectTeachers(input, input);
    }
    @AnatationTest
    @GetMapping("/getupTeachers")
    public List<Teachers> selectupTeachers() {
        System.out.println("-----select");
        return teacherService.selectupTeachers();
    }
    @AnatationTest
    @GetMapping("/getupallTeachers")
    public List<Teachers> selectupallTeachers() {
        System.out.println("select-----");
        return teacherService.selectupallTeachers();
    }
    @GetMapping("/updateTeacher")
    public int updateTeacher(@RequestParam(value = "key1") String id,
                             @RequestParam(value = "key2") String name,
                             @RequestParam(value = "key3") String genda,
                             @RequestParam(value = "key4") String position,
                             @RequestParam(value = "key5") String subject,
                             @RequestParam(value = "key6") String birthdate) {
        System.out.println("-----update--------");
        int nian=Integer.parseInt(birthdate.substring(0,4));
        int y=Integer.parseInt(birthdate.substring(5,7));
        int r=Integer.parseInt(birthdate.substring(8,10));
        if(nian%400==0||(nian%4==0&&nian%100!=0))yf[2]=29;
        else yf[2]=28;
        r++;
        if(r>yf[y]){
            y++;
            if(y==13){
                nian++;
                y=1;
                r=1;
            }else {
                r=1;
            }
        }
        Calendar cal=Calendar.getInstance();
        System.out.println((cal.get(Calendar.YEAR)-nian));
        return teacherService.updateTeacher(new Teacher(id, name, genda, position, subject, nian+"-"+y+"-"+r,(cal.get(Calendar.YEAR)-nian)+""));
    }

    @GetMapping("/deleteTeacher")
    public int teacherDelete(@RequestParam(value = "key1") String id) {
        System.out.println("---------delete---------");
        return teacherService.deleteTeacher(id);
    }
}
